package top.frsf.starter.impl;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import top.frsf.enums.GameStateEnum;
import top.frsf.object.env.*;
import top.frsf.panel.GamePanel;
import top.frsf.schedule.SumTimeTask;
import top.frsf.starter.Starter;

import java.awt.*;
import java.util.ArrayList;

import static top.frsf.constants.ImageConstant.Environment.*;

/**
 * 环境启动器
 *
 * @author FRSF
 * @since 2025/8/1 14:02
 */
@Data
@Slf4j
public class EnvStarter implements Starter {
    /**
     * 背景图片
     */
    private Background background = new Background(BACKGROUND, 0, 35, 1400, 600);
    /**
     * 购物盒子
     */
    private ShopBox shopBox = new ShopBox(SHOP_BOX, 200, 35, 446, 87);
    /**
     * 铲子盒子
     */
    private ShovelBox shovelBox = new ShovelBox(SHOVEL_BOX, 645, 35, 70, 72);
    /**
     * 铲子
     */
    private Shovel shovel = new Shovel(SHOVEL, 645, 35, 70, 72);
    /**
     * 阳光集合
     */
    private ArrayList<Sun> suns = new ArrayList<>();
    /**
     * 移除的阳光集合
     */
    private ArrayList<Sun> rmvSuns = new ArrayList<>();
    /**
     * 阳光值统计
     */
    private Integer sunsCount = 200;
    /**
     * 草地网格
     */
    private ArrayList<Grass> grids = new ArrayList<>();

    @Override
    public void init() {
        log.info("环境启动器初始化");
        // 阳光初始化
        SumTimeTask.start();
        // 草地网格辅助线初始化
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 9; j++) {
                grids.add(new Grass(80 + j * 80, 120 + i * 95));
            }
        }
    }

    @Override
    public void start(Graphics g) {
        if (GamePanel.state == GameStateEnum.START) {
            background.paintSelfImage(g);
        }

        if (GamePanel.state == GameStateEnum.RUNNING) {
            background.paintSelfImage(g);
            grids.forEach(grid -> grid.paintSelfRect(g));
            shopBox.paintSelfImage(g);
            shovelBox.paintSelfImage(g);
            shovel.paintSelfImage(g);
            this.removeSun();
            suns.forEach(sun -> sun.paintSelfImage(g));
            g.drawString(String.valueOf(sunsCount), 230, 110);
        }
    }

    /**
     * 移除已收集阳光
     */
    private void removeSun() {
        suns.removeAll(rmvSuns);
    }
}
